<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'渠道转化率'),
);
//客户列表
if($_RQ['op']=='list'){
	$return['params']=array('juese'=>$_RQ['juese']);
	$juese_id=$_RQ['juese']?:5;
	$workerids=$this->model('model')->getworkerids($juese_id);
	$params=array('worker'.$juese_id.'id'=>$workerids,'from_type'=>$_SESSION['store_type']==3?2:1,'storeid'=>$_SESSION['storeids']?:$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_SESSION['store_type']==3){
		if($_RQ['from2_type']){
			$types=array($_RQ['from2_type']=>$this->member_from2_type[$_RQ['from2_type']]);
		}elseif(!$_RQ['from1_type']){
			$types=$this->member_from2_type;
		}
	}else{
		if($_RQ['from1_type']){
			$types=array($_RQ['from1_type']=>$this->member_from1_type[$_RQ['from1_type']]);
		}elseif(!$_RQ['from2_type']){
			$types=$this->member_from1_type;
		}
	}
	$shmennumparams=$shmennum2params=$tingnumparams=$ting2numparams=$xuenumparams=$baonumparams=$tuifeenumparams=$params;
	$shmennum2params['status >']=0;
	$tuifeenumparams['status']=array(101,102,103);
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['to5time between']=array($time_range['tstart'],$time_range['tend']);
		$between=array($time_range['tstart'],$time_range['tend']);
		$return['params']['to5time']=$time_range;
	}
	$baonumparams=array('type'=>1,'status'=>1,'storeid'=>$_SESSION['storeids']?:$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	$baonumparams2=$nowfee=array('type'=>array(1,2),'status'=>1,'storeid'=>$_SESSION['storeids']?:$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	$tuifeenumparams=array('type'=>array(0,1),'status'=>4,'storeid'=>$_SESSION['storeids']?:$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if(!$between){
		$memberparams['to5time >']=0;
		$shmennumparams['zhun_time >']=0;
		$shmennum2params['zhun_time >']=0;
		$tingnumparams['ti5_time >']=0;
		$ting2numparams['ti6_time >']=0;
		$xuenumparams['ti7_time >']=0;
		$baonumparams['jiao_time >']=0;
		$baonumparams2['jiao_time >']=0;
		$tuifeenumparams['tuidaytime >']=0;
	}else{
		$memberparams['to5time between']=$between;
		$shmennumparams['zhun_time between']=$between;
		$shmennum2params['zhun_time between']=$between;
		$tingnumparams['ti5_time between']=$between;
		$ting2numparams['ti6_time between']=$between;
		$xuenumparams['ti7_time between']=$between;
		$baonumparams['jiao_time between']=$between;
		$baonumparams2['jiao_time between']=$between;
		$tuifeenumparams['tuidaytime between']=$between;
	}
	$list=array();
	if($types){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$starttime = mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
		$weektime = $starttime+7*24*3600;
		$monthtime=strtotime(date('Ym').'01');
		$status4=array(4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,32,33,34,35,36,37,38,39,40,41,42,100);
		$nowyue = mktime(0,0,0,date('m'),1,date('Y'));
		$nowfee['jiao_time >']=$nowyue;
		foreach($types as $k=>$v){
			if($_SESSION['store_type']==3){
				$baonumparams2['from2_type']=$nowfee['from2_type']=$shmennumparams['from2_type']=$shmennum2params['from2_type']=$tingnumparams['from2_type']=$ting2numparams['from2_type']=$xuenumparams['from2_type']=$baonumparams['from2_type']=$tuifeenumparams['from2_type']=$params['from2_type']=$k;
			}else{
				$nowfee['from1_type']=$baonumparams2['from1_type']=$shmennumparams['from1_type']=$shmennum2params['from1_type']=$tingnumparams['from1_type']=$ting2numparams['from1_type']=$xuenumparams['from1_type']=$baonumparams['from1_type']=$tuifeenumparams['from1_type']=$params['from1_type']=$k;
			}
			$item=array();
			$item['title']=$v;
			$item['memberall']=pdo_count('jy_yxw_member',$params);
			$item['shangmenall']=pdo_count('jy_yxw_member',$shmennumparams);
			$item['shangmenall2']=pdo_count('jy_yxw_member',$shmennum2params);
			$item['tingnum']=pdo_count('jy_yxw_member',$tingnumparams);
			$item['tingnum2']=pdo_count('jy_yxw_member',$ting2numparams);
			$item['xuenum']=pdo_count('jy_yxw_member',$xuenumparams);
			$item['tuinum']=pdo_count('jy_yxw_tuifee_log',$tuifeenumparams);
			if($item['memberall']>0){
				$item['shangmen2rate']=price($item['shangmenall2']*100/$item['memberall']);
			}
			if($item['shangmenall']>0){
				$item['ting1rate']=price($item['tingnum']*100/$item['shangmenall']);
			}
			if($item['tingnum']>0){
				$item['ting2rate']=price($item['tingnum2']*100/$item['tingnum']);
			}
			if($item['tingnum2']>0){
				$item['xue1rate']=price($item['xuenum']*100/$item['tingnum2']);
			}
			$item['baonum']=pdo_count('jy_yxw_infee_log',$baonumparams);
			if($item['xuenum']>0){
				$item['bao1rate']=price($item['baonum']*100/$item['xuenum']);
			}
			if($item['shangmenall']>0){
				$item['bao2rate']=price($item['baonum']*100/$item['shangmenall']);
			}
			$item['danfee']=pdo_getsum('jy_yxw_infee_log',$baonumparams2,'fee');
			$item['now']=pdo_getsum('jy_yxw_infee_log',$nowfee,'fee');
			$list[]=$item;
		}
	}
	$heji=array('title'=>'合计');
	foreach($list as $v){
		$heji['memberall'] +=$v['memberall'];
		$heji['shangmenall'] +=$v['shangmenall'];
		$heji['shangmenall2'] +=$v['shangmenall2'];
		$heji['tingnum'] +=$v['tingnum'];
		$heji['tingnum2'] +=$v['tingnum2'];
		$heji['xuenum'] +=$v['xuenum'];
		$heji['baonum'] +=$v['baonum'];
		$heji['tuinum'] +=$v['tuinum'];
		$heji['danfee'] +=$v['danfee'];
		$heji['now'] +=$v['now'];
	}
	if($heji['memberall']>0){
		$heji['shangmen2rate']=price($heji['shangmenall2']*100/$heji['memberall']);
	}
	if($heji['shangmenall']>0){
		$heji['ting1rate']=price($heji['tingnum']*100/$heji['shangmenall']);
	}
	if($heji['tingnum']>0){
		$heji['ting2rate']=price($heji['tingnum2']*100/$heji['tingnum']);
	}
	if($heji['tingnum2']>0){
		$heji['xue1rate']=price($heji['xuenum']*100/$heji['tingnum2']);
	}
	if($heji['xuenum']>0){
		$heji['bao1rate']=price($heji['baonum']*100/$heji['xuenum']);
	}
	if($heji['shangmenall']>0){
		$heji['bao2rate']=price($heji['baonum']*100/$heji['shangmenall']);
	}
	$list[]=$heji;
	$return['list']=$list;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'类型','type'=>'string'),
			array('name'=>'memberall','title'=>'客户','type'=>'string'),
			array('name'=>'shangmenall','title'=>'意向','type'=>'string'),
			array('name'=>'shangmenall2','title'=>'有效意向','type'=>'string'),
			array('name'=>'tingnum','title'=>'体验','type'=>'string'),
			array('name'=>'tingnum2','title'=>'体用','type'=>'string'),
			array('name'=>'xuenum','title'=>'试用','type'=>'string'),
			array('name'=>'baonum','title'=>'成交','type'=>'string'),
			array('name'=>'tuinum','title'=>'退成交','type'=>'string'),
			array('name'=>'shangmen2rate','title'=>'有效意向率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'ting1rate','title'=>'意向-体验率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'ting2rate','title'=>'体验体用率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'xue1rate','title'=>'体用试用率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'bao1rate','title'=>'试用成交率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'bao2rate','title'=>'总成交率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'now','title'=>'销售业绩','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'createtime','width'=>15,'title'=>'时间段','type'=>'time_range')
		),
	);
	if($_SESSION['store_type']==3){
		$htmls['search'][]=array('name'=>'from2_type','width'=>15,'title'=>'来源渠道','type'=>'radio','options'=>'from2_type');
	}else{
		$htmls['search'][]=array('name'=>'from1_type','width'=>15,'title'=>'来源渠道','type'=>'radio','options'=>'from1_type');
	}
	$return['from1_type']=$this->member_from1_type;
	$return['from2_type']=$this->member_from2_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	//客户列表
	$return['params']=array();
	$juese_id=$_RQ['juese']?:5;
	$workerids=$this->model('model')->getworkerids($juese_id);
	$params=array('worker'.$juese_id.'id'=>$workerids,'from_type'=>$_SESSION['store_type']==3?2:1,'storeid'=>$_SESSION['storeids']?:$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['from1_type']){
		$types=array($_RQ['from1_type']=>$this->member_from1_type[$_RQ['from1_type']]);
	}elseif(!$_RQ['from2_type']){
		$types=$this->member_from1_type;
	}
	$shmennumparams=$shmennum2params=$tingnumparams=$ting2numparams=$xuenumparams=$baonumparams=$tuifeenumparams=$params;
	$shmennum2params['status >']=0;
	$tuifeenumparams['status']=array(101,102,103);
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['to5time between']=array($time_range['tstart'],$time_range['tend']);
		$between=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$baonumparams=array('type'=>1,'status'=>1,'storeid'=>$_SESSION['storeids']?:$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	$baonumparams2=$nowfee=array('type'=>array(1,2),'status'=>1,'storeid'=>$_SESSION['storeids']?:$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	$tuifeenumparams=array('type'=>array(0,1),'status'=>4,'storeid'=>$_SESSION['storeids']?:$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if(!$between){
		$memberparams['to5time >']=0;
		$shmennumparams['zhun_time >']=0;
		$shmennum2params['zhun_time >']=0;
		$tingnumparams['ti5_time >']=0;
		$ting2numparams['ti6_time >']=0;
		$xuenumparams['ti7_time >']=0;
		$baonumparams['jiao_time >']=0;
		$baonumparams2['jiao_time >']=0;
		$tuifeenumparams['tuidaytime >']=0;
	}else{
		$memberparams['to5time between']=$between;
		$shmennumparams['zhun_time between']=$between;
		$shmennum2params['zhun_time between']=$between;
		$tingnumparams['ti5_time between']=$between;
		$ting2numparams['ti6_time between']=$between;
		$xuenumparams['ti7_time between']=$between;
		$baonumparams['jiao_time between']=$between;
		$baonumparams2['jiao_time between']=$between;
		$tuifeenumparams['tuidaytime between']=$between;
	}
	$list=array();
	if($types){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$starttime = mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
		$weektime = $starttime+7*24*3600;
		$monthtime=strtotime(date('Ym').'01');
		$status4=array(4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,32,33,34,35,36,37,38,39,40,41,42,100);
		foreach($types as $k=>$v){
			$baonumparams2['from1_type']=$shmennumparams['from1_type']=$shmennum2params['from1_type']=$tingnumparams['from1_type']=$ting2numparams['from1_type']=$xuenumparams['from1_type']=$baonumparams['from1_type']=$tuifeenumparams['from1_type']=$params['from1_type']=$k;
			$item=array();
			$item['title']=$v;
			$item['memberall']=pdo_count('jy_yxw_member',$params);
			$item['shangmenall']=pdo_count('jy_yxw_member',$shmennumparams);
			$item['shangmenall2']=pdo_count('jy_yxw_member',$shmennum2params);
			$item['tingnum']=pdo_count('jy_yxw_member',$tingnumparams);
			$item['tingnum2']=pdo_count('jy_yxw_member',$ting2numparams);
			$item['xuenum']=pdo_count('jy_yxw_member',$xuenumparams);
			$item['tuinum']=pdo_count('jy_yxw_tuifee_log',$tuifeenumparams);
			if($item['memberall']>0){
				$item['shangmen2rate']=price($item['shangmenall2']*100/$item['memberall']);
				$item['shangmen2rate'] .='%';
			}
			if($item['shangmenall']>0){
				$item['ting1rate']=price($item['tingnum']*100/$item['shangmenall']);
				$item['ting1rate'] .='%';
			}
			if($item['tingnum']>0){
				$item['ting2rate']=price($item['tingnum2']*100/$item['tingnum']);
				$item['ting2rate'] .='%';
			}
			if($item['tingnum2']>0){
				$item['xue1rate']=price($item['xuenum']*100/$item['tingnum2']);
				$item['xue1rate'] .='%';
			}
			$item['baonum']=pdo_count('jy_yxw_infee_log',$baonumparams);
			if($item['xuenum']>0){
				$item['bao1rate']=price($item['baonum']*100/$item['xuenum']);
			}
			if($item['shangmenall']>0){
				$item['bao2rate']=price($item['baonum']*100/$item['shangmenall']);
			}
			$item['danfee']=pdo_getsum('jy_yxw_infee_log',$baonumparams2,'fee');
			$item['now']=pdo_getsum('jy_yxw_infee_log',$nowfee,'fee');
			$list[]=$item;
		}
	}
	$params=array('from_type'=>2,'storeid'=>$_SESSION['storeids']?:$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['from2_type']){
		$types=array($_RQ['from2_type']=>$this->member_from2_type[$_RQ['from2_type']]);
	}elseif(!$_RQ['from1_type']){
		$types=$this->member_from2_type;
	}
	if($types){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$starttime = mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
		$weektime = $starttime+7*24*3600;
		$monthtime=strtotime(date('Ym').'01');
		$status4=array(4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,32,33,34,35,36,37,38,39,40,41,42,100);
		$nowyue = mktime(0,0,0,date('m'),1,date('Y'));
		$nowfee['jiao_time >']=$nowyue;
		foreach($types as $k=>$v){
			if($_SESSION['store_type']==3){
				$baonumparams2['from2_type']=$nowfee['from2_type']=$shmennumparams['from2_type']=$shmennum2params['from2_type']=$tingnumparams['from2_type']=$ting2numparams['from2_type']=$xuenumparams['from2_type']=$baonumparams['from2_type']=$tuifeenumparams['from2_type']=$params['from2_type']=$k;
			}else{
				$nowfee['from1_type']=$baonumparams2['from1_type']=$shmennumparams['from1_type']=$shmennum2params['from1_type']=$tingnumparams['from1_type']=$ting2numparams['from1_type']=$xuenumparams['from1_type']=$baonumparams['from1_type']=$tuifeenumparams['from1_type']=$params['from1_type']=$k;
			}
			$item=array();
			$item['title']=$v;
			$item['memberall']=pdo_count('jy_yxw_member',$params);
			$item['shangmenall']=pdo_count('jy_yxw_member',$shmennumparams);
			$item['shangmenall2']=pdo_count('jy_yxw_member',$shmennum2params);
			$item['tingnum']=pdo_count('jy_yxw_member',$tingnumparams);
			$item['tingnum2']=pdo_count('jy_yxw_member',$ting2numparams);
			$item['xuenum']=pdo_count('jy_yxw_member',$xuenumparams);
			$item['baonum']=pdo_count('jy_yxw_member',$baonumparams);
			$item['tuinum']=pdo_count('jy_yxw_tuifee_log',$tuifeenumparams);
			if($item['memberall']>0){
				$item['shangmen2rate']=price($item['shangmenall2']*100/$item['memberall']);
				$item['shangmen2rate'] .='%';
			}
			if($item['shangmenall']>0){
				$item['ting1rate']=price($item['tingnum']*100/$item['shangmenall']);
				$item['ting1rate'] .='%';
			}
			if($item['tingnum']>0){
				$item['ting2rate']=price($item['tingnum2']*100/$item['tingnum']);
				$item['ting2rate'] .='%';
			}
			if($item['tingnum2']>0){
				$item['xue1rate']=price($item['xuenum']*100/$item['tingnum2']);
				$item['xue1rate'] .='%';
			}
			$item['baonum']=pdo_count('jy_yxw_infee_log',$baonumparams);
			if($item['xuenum']>0){
				$item['bao1rate']=price($item['baonum']*100/$item['xuenum']);
			}
			if($item['shangmenall']>0){
				$item['bao2rate']=price($item['baonum']*100/$item['shangmenall']);
			}
			$item['danfee']=pdo_getsum('jy_yxw_infee_log',$baonumparams2,'fee');
			$list[]=$item;
		}
	}
	$heji=array('title'=>'合计');
	foreach($list as $v){
		$heji['memberall'] +=$v['memberall'];
		$heji['shangmenall'] +=$v['shangmenall'];
		$heji['shangmenall2'] +=$v['shangmenall2'];
		$heji['tingnum'] +=$v['tingnum'];
		$heji['tingnum2'] +=$v['tingnum2'];
		$heji['xuenum'] +=$v['xuenum'];
		$heji['baonum'] +=$v['baonum'];
		$heji['tuinum'] +=$v['tuinum'];
		$heji['danfee'] +=$v['danfee'];
		$heji['now'] +=$v['now'];
	}
	if($heji['memberall']>0){
		$heji['shangmen2rate']=price($heji['shangmenall2']*100/$heji['memberall']);
	}
	if($heji['shangmenall']>0){
		$heji['ting1rate']=price($heji['tingnum']*100/$heji['shangmenall']);
	}
	if($heji['tingnum']>0){
		$heji['ting2rate']=price($heji['tingnum2']*100/$heji['tingnum']);
	}
	if($heji['tingnum2']>0){
		$heji['xue1rate']=price($heji['xuenum']*100/$heji['tingnum2']);
	}
	if($heji['xuenum']>0){
		$heji['bao1rate']=price($heji['baonum']*100/$heji['xuenum']);
	}
	if($heji['shangmenall']>0){
		$heji['bao2rate']=price($heji['baonum']*100/$heji['shangmenall']);
	}
	$list[]=$heji;
	$header=array(
		'title'=>'类型',
		'memberall'=>'客户',
		'shangmenall'=>'意向',
		'shangmenall2'=>'有效意向',
		'tingnum'=>'体验',
		'tingnum2'=>'体用',
		'xuenum'=>'试用',
		'baonum'=>'成交',
		'tuinum'=>'退成交',
		'shangmen2rate'=>'有效意向率',
		'ting1rate'=>'意向-体验率',
		'ting2rate'=>'体验体用率',
		'xue1rate'=>'体用试用率',
		'bao1rate'=>'试用成交率',
		'bao2rate'=>'总成交率',
		'danfee'=>'成交金额',
	);
	model('core/tools')->export2excel($header,$list,'渠道转化率数据');
}